﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DALBatteryModel : IDALBatteryModel, IDisposable
    {
        private CarBatteryEntities context;
        private bool disposed = false;

        public DALBatteryModel(CarBatteryEntities context)
        {
            this.context = context;
        }

        public IEnumerable<BatteryModel> GetBatteryModels()
        {
            return context.BatteryModel.AsEnumerable();
        }

        public BatteryModel GetBatteryModelByID(int bModelID)
        {
            return context.BatteryModel.Find(bModelID);
        }

        public BatteryModel GetBatteryModelByName(string bModelName)
        {
            return context.BatteryModel.FirstOrDefault(x => x.model.Equals(bModelName));
        }

        public void InsertBatteryModel(BatteryModel bModel)
        {
            context.BatteryModel.Add(bModel);
        }

        public void DeleteBatteryModel(int bModelID)
        {
            context.BatteryModel.Remove(GetBatteryModelByID(bModelID));
        }

        public void UpdateBatteryModel(BatteryModel bModel)
        {
            context.Entry(bModel).State = EntityState.Modified;
        }

        public int Save()
        {
            return context.SaveChanges();
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                    context.Dispose();
            }

            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}